package com.xiyou.luckdraw.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiyou.luckdraw.api.converter.UserTntegralLogConverter;
import com.xiyou.luckdraw.api.dal.entitys.UserInfo;
import com.xiyou.luckdraw.api.dal.entitys.UserTntegralLog;
import com.xiyou.luckdraw.api.dal.mapper.luckdraw.UserTntegralLog1Mapper;
import com.xiyou.luckdraw.api.dal.mapper.luckyelite.UserTntegralLog2Mapper;
import com.xiyou.luckdraw.api.dto.user.*;
import com.xiyou.luckdraw.api.service.IUserTntegralLogService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.exception.ExceptionProcessorUtils;
import com.xiyou.luckdraw.common.utils.DateUtils;
import com.xiyou.luckdraw.common.utils.ReqUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserTntegralLogServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-21 16:17
 */
@Slf4j
@Service
public class UserTntegralLogServiceImpl implements IUserTntegralLogService {

    @Autowired
    private UserTntegralLog1Mapper userTntegralLog1Mapper;
    @Autowired
    private UserTntegralLog2Mapper UserTntegralLog2Mapper;
    @Autowired
    UserTntegralLogConverter tntegralLogConverter;

    @Override
    public UserTntegralLogListResponse getList(UserTntegralLogListRequest request) {
        log.error("Begin UserTntegralLogServiceImpl.getList request : {}", request);
        UserTntegralLogListResponse response = new UserTntegralLogListResponse();
        try{
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            String datasourceType = ReqUtils.getDatasourceType();
            List<UserTntegralLog> list = null;
            if (StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)) {
                list = userTntegralLog1Mapper.selectListByUserId(request.getUserId());
            } else {
                list = UserTntegralLog2Mapper.selectListByUserId(request.getUserId());
            }
            PageInfo pageInfo = new PageInfo(list);
           List<UserTntegralLogDto> dtoList = tntegralLogConverter.UserTntegralLogs2Dto(list);
            response.setList(dtoList);
            response.setTotal(pageInfo.getTotal());
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());

        }catch (Exception e){
            log.error("UserTntegralLogServiceImpl.getList Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        response.setCode(ResultCodeConstants.SUCCESS.getCode());
        response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public CountNewVoucherResponse selectCountNewVoucher() {
        log.error("Begin UserTntegralLogServiceImpl.selectCountNewVoucher ");
        CountNewVoucherResponse response = new CountNewVoucherResponse();
        String datasourceType = ReqUtils.getDatasourceType();
        try {
            List<Map> result = null;
            if (StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)) {
                result = userTntegralLog1Mapper.selectCountNewVoucher();
            } else {
                result = UserTntegralLog2Mapper.selectCountNewVoucher();
            }
            response.setList(result);
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("UserTntegralLogServiceImpl.selectCountNewVoucher Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
